from time import sleep

from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By

from Common.Utils import BlogDriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class Detail:
    url = ""
    driver = ""


    def __init__(self):
        self.url = "http://127.0.0.1:58080/index.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)

    def Browse(self):
        BlogDriver.getScreenshort()
        sleep(2)

        #统计列表页的一个帖子的浏览、点赞、消息
        browse=self.driver.find_element(By.CSS_SELECTOR,
                                        "#artical-items-body > div:nth-child(1) > div > div.col > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(1)"
                                        ).text
        like=self.driver.find_element(By.CSS_SELECTOR,
                                      "#artical-items-body > div:nth-child(1) > div > div.col > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(2)"
                                      ).text
        talk=self.driver.find_element(By.CSS_SELECTOR,
                                      "#artical-items-body > div:nth-child(1) > div > div.col > div.text-muted.mt-2 > div > div.col-auto.d-none.d-md-inline > ul > li:nth-child(3)"
                                      ).text


        self.driver.find_element(By.CSS_SELECTOR,
                                 "#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong"
                                 ).click()
        #浏览量变化测试
        new_browse=self.driver.find_element(By.CSS_SELECTOR,
                                 "#details_article_visitCount"
                                 ).text
        # print(f"{int(browse) + 1}")
        # print(f"{new_browse}")
        assert int(new_browse)==int(browse) + 1,print(f"浏览数量不一致，旧的数量为{browse}，新的为{new_browse}")
        BlogDriver.getScreenshort()
        #点赞测试
        self.driver.find_element(By.CSS_SELECTOR,"#details_btn_like_count").click()

        sleep(2)
        new_like=self.driver.find_element(By.CSS_SELECTOR,
                                 "#details_article_likeCount"
                                 ).text


        assert int(new_like) == int(like) + 1,print(f"点赞数量不一致，旧的数量为{like}，新的为{new_like}")
        BlogDriver.getScreenshort()

        new_talk = [talk.text for talk in self.driver.find_elements(
            By.CSS_SELECTOR, "#details_reply_area .markdown-body p"
        )]
        new_talk = len(new_talk)
        assert int(talk) == int(new_talk), print(f"回复数量不一致，旧的数量为{talk}，新的为{new_talk}")
        sleep(2)
        BlogDriver.getScreenshort()
        self.driver.execute_script("window.scrollTo(0, 400);")
        sleep(1)
        BlogDriver.getScreenshort()

        self.driver.execute_script("window.scrollTo(0, -400);")

        # 编辑使用
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto.justify-content-end > div > div:nth-child(2) > div"
                                 ).click()
        sleep(1)
        BlogDriver.getScreenshort()



        self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

        self.driver.find_element(By.CSS_SELECTOR,
                                 "#edit_article_submit"
                                 ).click()
        BlogDriver.getScreenshort()

    def Delete(self):
        BlogDriver.getScreenshort()
        # 取消删除
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#artical-items-body > div:nth-child(2) > div > div.col > div.text-truncate > a > strong"
                                 ).click()

        self.driver.find_element(By.CSS_SELECTOR,
                                 "#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto.justify-content-end > div > div:nth-child(3) > div > a"
                                 ).click()

        sleep(2)
        BlogDriver.getScreenshort()
        self.driver.find_element(By.CSS_SELECTOR,"#details_delete_modal > div > div > div.modal-footer > div > div > div:nth-child(1) > a").click()
        sleep(1)
        BlogDriver.getScreenshort()

        # 确定删除
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#bit-forum-content > div.page-body > div > div > div:nth-child(1) > div.col-9.card.card-lg > div.card-footer.bg-transparent.mt-auto.justify-content-end > div > div:nth-child(3) > div > a"
                                 ).click()
        sleep(2)
        BlogDriver.getScreenshort()
        self.driver.find_element(By.CSS_SELECTOR,
                                 "#details_artile_delete"
                                 ).click()
        sleep(1)

        BlogDriver.getScreenshort()







